package com.example.question.tree.tree3;

import com.example.question.tree.tree1.TreeNode;

import java.util.HashMap;
import java.util.Map;

/**
 * 后序和中序构建二叉树
 */
public class Code19_106 {
    public static void main(String[] args) {
        Code19_106 code = new Code19_106();
        int[] inorder = new int[]{9, 3, 15, 20, 7};
        int[] postorder = new int[]{9, 15, 7, 20, 3};
        TreeNode treeNode = code.buildTree(inorder, postorder);
    }

    //1 8    9   7 33     3     15 20 11
    //1 8   33 7    9             15 11 20 3
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for (int i = 0; i < inorder.length; i++) {
            inorderIdxMap.put(inorder[i], i);
        }
//        this.inorder = inorder;
        this.postorder = postorder;
        this.index = postorder.length - 1;
//        return buildTree(0, postorder.length - 1);
        return null;
    }

    Map<Integer, Integer> inorderIdxMap = new HashMap<>();
    private int index;
    private int[] postorder;

    private TreeNode buildTree(int left, int right, int index) {
        if (left > right || index < 0) {
            return null;
        }
        TreeNode node = new TreeNode(postorder[index]);
        Integer mid = inorderIdxMap.get(postorder[index]);
//        index--;
//        node.right = buildTree(left, mid - 1);
//        node.left = buildTree(mid + 1, right);
        return node;
    }
}
